﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using Dost.Khcn.Data.EntityModel;
using Dost.Khcn.Data.Interfaces;

namespace Dost.Khcn.Data.Implements
{
    public class CongTrinhNghienCuuDao : ICongTrinhNghienCuuDao
    {
        public List<CongTrinhNghienCuu> GetCongTrinhNghienCuus(int chuyenGiaId)
        {
            using (var db = new NLKHCNDataEntities())
            {
                return db.CongTrinhNghienCuus.Where(x => x.ChuyenGiaId == chuyenGiaId).ToList();
            }
        }

        public CongTrinhNghienCuu Create(CongTrinhNghienCuu congTrinhNghienCuu)
        {
            using (var db = new NLKHCNDataEntities())
            {
                db.CongTrinhNghienCuus.Add(congTrinhNghienCuu);
                db.SaveChanges();
            }

            return congTrinhNghienCuu;
        }

        public void Update(CongTrinhNghienCuu congTrinhNghienCuu)
        {
            using (var db = new NLKHCNDataEntities())
            {
                db.CongTrinhNghienCuus.Attach(congTrinhNghienCuu);
                db.Entry(congTrinhNghienCuu).State = EntityState.Modified;
                db.SaveChanges();
            }
        }

        public void Delete(int id)
        {
            if (id <= 0) return;
            using (var db = new NLKHCNDataEntities())
            {
                var deletedItem = new CongTrinhNghienCuu {Id = id};
                db.CongTrinhNghienCuus.Attach(deletedItem);
                db.Entry(deletedItem).State = EntityState.Deleted;
                db.SaveChanges();
            }
        }
    }
}
